﻿#define MySql
#undef MSSql

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
using System.Text.RegularExpressions;

using MySql.Data.MySqlClient;

namespace ukeasbjWeb
{
    public partial class ApplicationEdit : Bonus ///System.Web.UI.Page
    {
        //合同主键
        string sSeries_No;
        string sSeries_No_Application;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            Common_SXD.Is_LongIn(Response, Session, Request);
            if (!IsPostBack)
            {

               
            }



            Session.Timeout = 118;
            ///////////// 读出合同主键
            sSeries_No = Request["Series_No"];
            ///sSeries_No = sSeries_No.Replace("%3a", ":");
            Regex Flag_ApplicantionEdit = new Regex("([0-9]{1,}):([a-zA-Z_]{1,}):([0-9]{0,})");
            /// 控制字符串格式分为三个部分，数字（Series_No）：单词（申请人信息操作）或（申请个案信息操作）: 数字（Series_No_Application）
            /// （申请人信息操作）: NewOne_Applicant, Edit_Applicant, ReadOnly_Applicant
            /// （申请个案信息操作）: Edit_Application, ReadOnly_Application
            string[] temp;

            if (Flag_ApplicantionEdit.IsMatch(sSeries_No))
            {
                temp = Flag_ApplicantionEdit.Split(sSeries_No);
                sSeries_No = temp[1];
                sSeries_No_Application = temp[3];
                Label_Series_No.Text = sSeries_No;
                Label_Series_No_Applications.Text = sSeries_No_Application;
                
                if (temp[2] == "NewOne_Applicant")
                {//添加申请人
                    if (!IsPostBack)
                        Init_NewOne_Applicant();
                        
                        ///temp[2];
                    
                        ///List_Applications();
                    
                    return;
                }
                else if (temp[2] == "Edit_Applicant")
                {
                    if (!IsPostBack)
                        Init_Edit_Applicant();
                    
                    //Label_Msg.Text = sSeries_No;
                    /// 初始化更换顾问、文案网页的链接    
                    ///Common_SXD.Init_Replace_C_W(Table_Replace_C_W, Label_Msg.Text.Trim());
                    return;
                    
                }
                else if (temp[2] == "ReadOnly_Applicant")
                {
                    if (!IsPostBack)
                        Init_ReadOnly_Applicant();

                    //Label_Msg.Text = sSeries_No;
                    /// 初始化更换顾问、文案网页的链接    
                    ///Common_SXD.Init_Replace_C_W(Table_Replace_C_W, Label_Msg.Text.Trim());
                    return;

                }
                else if (temp[2] == "Edit_Application")
                {
                    if (!IsPostBack)
                        Init_Edit_Application();
                //Label_Msg.Text = sSeries_No;
                /// 初始化更换顾问、文案网页的链接    
                ///Common_SXD.Init_Replace_C_W(Table_Replace_C_W, Label_Msg.Text.Trim());
                    return;

                }
                else if (temp[2] == "ReadOnly_Application")
                {
                    sSeries_No = temp[2];
                    if (!IsPostBack)
                        Init_ReadOnly_Application();
                    //Init_CheckFinishedTerminationRefund();
                //Label_Msg.Text = sSeries_No;
                /// 初始化更换顾问、文案网页的链接    
                ///Common_SXD.Init_Replace_C_W(Table_Replace_C_W, Label_Msg.Text.Trim());
                    return;

                }
            
            }

            
            
        }

        /// <summary>
        /// Drop_Writer_Application 控件初始化 
        /// Drop_Net_Application 控件初始化 
        /// </summary>
        private void InitializeDrop_Writer()
        {
            ///SqlConnection sqlCnn = new SqlConnection(strCnn);
            // 载文案列表，绑定到 Drop_Writer 中
            string sSql;
            ///string temp = Session["Office"].ToString().Trim();
            sSql = Common_SXD.sSql_InitializeDrop_Writer(TextBox_Office.Text);

            Common_SXD.Init_DropDownList(Drop_Writer_Application, "Employees", sSql);
            Common_SXD.Init_DropDownList(Drop_Net_Application, "Employees", sSql);

        }

        /// <summary>
        /// 课程文本框、选择框初始化 
        /// </summary>
        private void Initialize_Course()
        {
            Drop_Course_Application.Enabled = false;
            if (CheckBox_School_Application.Checked)
            {   /// 个案类型选定之后，才能建立学校清单
                ///Drop_Type_Application.Enabled = true;
                if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "长期课程1")
                {
                    // 课程选择菜单初始化
                    string temp;
                    temp = "select * from Courses_Schools where Course like '无' or School like '" + Double_Single_Quotation(Drop_College_Application.SelectedItem.Text.Trim()) + "' order by Course asc";
                    Common_SXD.Init_DropDownList(Drop_Course_Application, "Course", temp);

                    if (CheckBox_Course_Application.Checked)
                    {
                        Drop_Course_Application.Enabled = true;
                        Drop_Course_Application.Visible = true;

                        ///Drop_College_Application.Enabled = false;

                        CheckBox_Course_Application.Enabled = true;
                    }
                    else
                    {
                        Drop_Course_Application.Enabled = false;
                        Drop_Course_Application.Visible = true;
                        ///if (CheckBox_School_Application.Checked)
                            ///Drop_College_Application.Enabled = true;

                        CheckBox_Course_Application.Enabled = true;

                    }

                }
                else ///if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "长期课程2")
                {
                    CheckBox_Course_Application.Visible = false;
                    TextBox_Course_Application.Enabled = true;
                    Drop_Course_Application.Visible = false;
                }/*
                else if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "学前语言课程")
                {
                    
                    CheckBox_Course_Application.Visible = false;
                    TextBox_Course_Application.Enabled = true;
                    Drop_Course_Application.Visible = false;
                }
                else if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "语言课程")
                {
                    
                    CheckBox_Course_Application.Visible = false;
                    TextBox_Course_Application.Enabled = true;
                    Drop_Course_Application.Visible = false;
                }*/
            }

        }


        /// <summary>
        /// 学校清单初始化 
        /// </summary>
        private void Initialize_School()
        {
            Drop_Course_Application.Enabled = false;
            if (CheckBox_School_Application.Checked)
            {   /// 个案类型选定之后，才能建立学校清单
                ///Drop_Type_Application.Enabled = true;
                if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "长期课程1")
                {
                    // 申请院校，首先绑定长期课程院校1
                    Common_SXD.Init_DropDownList(Drop_College_Application, "School",
                        ///"select * from Schools1 order by School asc");
                        "select * from Schools_HTS where Is_College1_Degree_courses = 1 order by School asc");
                    CheckBox_Disp_Sleeped.Checked = false;
                    ///Label_Tab_Applicant.Text = "";
                    ///Label_Tab_Application.Text = "";
                    Label_College_Application.Text = "长期课程院校1";
                    Label_Course_Application.Text = "长期课程1(上下相同)";
                    
                    if (CheckBox_Course_Application.Checked)
                    {
                        ///Drop_Course_Application.Enabled = true;
                        ///Drop_Course_Application.Visible = true;

                        Drop_College_Application.Enabled = false;

                        ///CheckBox_Course_Application.Enabled = true;
                    }
                    else
                    {
                        ///Drop_Course_Application.Enabled = false;
                        ///Drop_Course_Application.Visible = true;
                        if (CheckBox_School_Application.Checked)
                            Drop_College_Application.Enabled = true;

                        ///CheckBox_Course_Application.Enabled = true;

                    }
            
                }
                else if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "长期课程2")
                {
                    Common_SXD.Init_DropDownList(Drop_College_Application, "School",
                        ///"select * from Schools2 order by School asc");
                        "select * from Schools_HTS where Is_College2_Degree_courses = 1 order by School asc");
                    Label_College_Application.Text = "长期课程院校2";
                    Label_Course_Application.Text = "长期课程2";
                    Drop_College_Application.Enabled = true;

                    ///CheckBox_Course_Application.Visible = false;
                    ///TextBox_Course_Application.Enabled = true;
                    ///Drop_Course_Application.Visible = false;
                }
                else if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "学前语言课程")
                {
                    Common_SXD.Init_DropDownList(Drop_College_Application, "School",
                        ///"select * from Schools1 order by School asc");
                        "select * from Schools_HTS where Is_College1_Degree_courses = 1 order by School asc");
                    Label_College_Application.Text = "学前语言课程院校";
                    Label_Course_Application.Text = "学前语言课程";
                    Drop_College_Application.Enabled = true;

                    ///CheckBox_Course_Application.Visible = false;
                    ///TextBox_Course_Application.Enabled = true;
                    ///Drop_Course_Application.Visible = false;
                }
                else if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "语言课程")
                {
                    //  语言学校
                    Common_SXD.Init_DropDownList(Drop_College_Application, "School",
                        ///"select * from Language_Schools order by School asc");
                        "select * from Schools_HTS where Is_College_Language_School = 1 order by School asc");
                    Label_College_Application.Text = "学前语言课程院校";
                    Label_Course_Application.Text = "学前语言课程";
                    Drop_College_Application.Enabled = true;

                    ///CheckBox_Course_Application.Visible = false;
                    ///TextBox_Course_Application.Enabled = true;
                    ///Drop_Course_Application.Visible = false;
                }
            }
            
        }

        /////////////////////////////
        /// <summary>
        /// 更新申请个案信息。
        /// </summary>
        /// <remarks>
        /// 只有修改数据的控件有效（enable）时，该数据才能被更新。
        /// </remarks>
        public void Update_Application()
        {
            //申请个案信息写入数据库
            string sSql = "Update Applications Set";

            ////////////////////
            /// 申请个案信息
            sSql = sSql_Add_TextBox(TextBox_Result_Application, "Result_Application", sSql);
            sSql = sSql_Add_TextBox(TextBox_User_Application, "User_Application", sSql);
            sSql = sSql_Add_TextBox(TextBox_Password_User_Application, "Password_User_Application", sSql);

            sSql = sSql_Add_DateTime(TextBox_Date_Submit_Application, "Date_Submit_Application", sSql);
            sSql = sSql_Add_DateTime(TextBox_Date_Result_Application, "Date_Result_Application", sSql);

            sSql = sSql_Add_DropDownList(Drop_Writer_Application, TextBox_Writer_Application, "Writer_Application", sSql);
            sSql = sSql_Add_DropDownList(Drop_Net_Application, TextBox_Net_Application, "Net_Application", sSql);

            sSql = sSql_Add_DropDownList(Drop_College_Application, "College_Application", sSql);

            if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "长期课程1")
            {
                sSql = sSql_Add_DropDownList(Drop_Course_Application, "Course_Application", sSql);
            }
            else
                sSql = sSql_Add_TextBox(TextBox_Course_Application, "Course_Application", sSql);

            sSql = sSql_Add_DropDownList(Drop_Type_Application, "Type_Application", sSql);

            sSql = sSql_Add_Checkd(CheckBox_Is_Active_Application, "Is_Active", sSql);

            sSql = sSql_Add_TextBox(TextBox_ID_Application, "ID_Application", sSql);
            /// 最后一个 set 组
            sSql += " Notes_Application = '" + Double_Single_Quotation(TextBox_Notes_Application.Text.Trim()) + "' ";
            ///////////////////////////////////////


            sSql += " where Series_No_Applications = " + "'" + Label_Series_No_Applications.Text.Trim() + "'";
#if MSSql
            Common_SXD.MSSql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#elif MySql
            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#endif

        }// end of Update_Application()


        /////////////////////////////
        /// <summary>
        /// 更新合同数据库申请人数据记录（不是新数据）。
        /// </summary>
        /// <remarks>
        /// 只有修改数据的控件有效（enable）时，该数据才能被更新。
        /// </remarks>
        public void Update_Applicant()
        {
            //合同信息写入数据库
            string sSql = "Update Contract Set";

            ////////////////////
            /// 申请人信息
            sSql = sSql_Add_TextBox(TextBox_Course_Applicant, "Course_Applicant", sSql);
            sSql = sSql_Add_DateTime(TextBox_Date_Birth, "Date_Birth", sSql);

            sSql = sSql_Add_DropDownList(Drop_Level_Application, "Level_Application", sSql);
            sSql = sSql_Add_TextBox(TextBox_University_Applicant_Learning, "University_Applicant_Learning", sSql);
            sSql = sSql_Add_TextBox(TextBox_Course_Learning, "Course_Learning", sSql);
            sSql = sSql_Add_TextBox(TextBox_Grade_Applicant, "Grade_Applicant", sSql);
            sSql = sSql_Add_TextBox(TextBox_Mobile_Applicant, "Mobile_Applicant", sSql);
            sSql = sSql_Add_TextBox(TextBox_Email_Student_Applicant, "Email_Student_Applicant", sSql);
            sSql = sSql_Add_TextBox(TextBox_Email_Student, "Email_Student", sSql);
            sSql = sSql_Add_TextBox(TextBox_Password_Email_Student_Applicant, "Password_Email_Student_Applicant", sSql);

            /// 最后一个 set 组
            sSql += " Notes_Applicant = '" + TextBox_Notes_Applicant.Text.Trim() + "' ";
            ///////////////////////////////////////


            sSql += " where Series_No = " + "'" + Label_Series_No.Text.Trim() + "'";
#if MSSql
            Common_SXD.MSSql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#elif MySql
            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#endif
        
        }// end of Update_Contract()


        /// <summary>
        /// 申请个案列表
        /// </summary>
        private void List_Applications(/*string myOffice */)
        {
            ///string sSearchValue = TextBox_SearchValue.Text.Trim();

            ///string sDDList_Operator = DDList_Operator.SelectedItem.Text;
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            bool bIs_Marketing = Convert.ToBoolean(Session["Is_Marketing"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);
            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Is_Frontline_Agent"]);


            DateTime dNow = DateTime.Now;

            ///Label_Consultation_Follow_up.Text = "已有的跟进记录：";

            string sSql;

            int tDay = DateTime.Now.Day;
            int tMonth = DateTime.Now.Month;
            int tYear = DateTime.Now.Year - 1;

            string sDay_Minus1Year = Convert.ToString(tYear) + "-" + Convert.ToString(tMonth) + "-" + Convert.ToString(tDay);

            
            ///测试
            ///Label_Series_No_Applicants.Text = "9999";
            Session.Timeout = 118;
            sSql = "select * from Applications";
            sSql += " where  Series_No like '" + Label_Series_No.Text.Trim() + "' ";
            if (!CheckBox_Disp_Sleeped.Checked)
                sSql += "and ( Is_Active = 1  or  Is_Active is null )  ";

            ///}


            /////排序方式设定开始

            sSql += " order by Series_No_Applications DESC";
            
            //////排序方式结束

#if MSSql
            SqlConnection sqlCnn = new SqlConnection();
            SqlDataReader sqlReader = Common_SXD.Open_MSSqlDataReader(sSql, sqlCnn, "打开数据库出错：", Response);
            int nCol = sqlReader.FieldCount;
#elif MySql
            MySqlConnection MySqlconn = new MySqlConnection();
            MySqlDataReader MysqlReader = Common_SXD.Open_MySqlDataReader(sSql, MySqlconn, "打开数据库出错：", Response);
            int nCol = MysqlReader.FieldCount;
#endif

            //            }
            //清除表中的内容
            Table_List_Applications.Rows.Clear();
            //首先生成标题行
            {
                HtmlTableRow row = new HtmlTableRow();
                for (int i = 0; i < nCol; i++)
                {
                    // Create a new cell and add it to the Cells collection.
                    HtmlTableCell cell = new HtmlTableCell();
                    ///string temp = "";
#if MSSql
                    string temp = Common_SXD.English_Chinese(sqlReader.GetName(i));
#elif MySql
                    string temp = Common_SXD.English_Chinese(MysqlReader.GetName(i));

#endif
                    
                    cell.Controls.Add(new LiteralControl(temp.Trim()));
                    row.Cells.Add(cell);
                }
                {
                    HtmlTableCell cell = new HtmlTableCell();
                    cell.Controls.Add(new LiteralControl("修改"));
                    row.Cells.Add(cell);
                }
                Table_List_Applications.Rows.Add(row);
                row.BgColor = "Tan";
            }

            // 依次读出数据
            int j = 0;
            
#if MSSql
            while (sqlReader.Read())
#elif MySql
            while (MysqlReader.Read())
#endif
            {
                HtmlTableRow row = new HtmlTableRow();
#if MSSql
                String sNo_Series_Lead = sqlReader.GetInt64(0).ToString().Trim();
#elif MySql
                String sNo_Series_Lead = MysqlReader.GetInt64(0).ToString().Trim();
#endif

                ////////////////////////////////
                // Provide a different background color for alternating rows.
                if (j % 2 == 1)
                    row.BgColor = "Gainsboro";

                // Iterate through the cells of a row.
                {
                    HtmlTableCell cell = new HtmlTableCell();
                    /*
                    cell.Controls.Add(new LiteralControl("<a href=\"JavaScript:comOpen('./LeadEdit.aspx?Series_No_Lead=ReadOnly:" +
                        sNo_Series_Lead.Trim() +
                        "',900,620);\">只读" +
                        sNo_Series_Lead.Trim() +
                        "</a>"));*/
                    cell.Controls.Add(new LiteralControl("<a>" +
                        sNo_Series_Lead.Trim() +
                        "</a>"));

                    row.Cells.Add(cell);
                }

                for (int i = 1; i < nCol; i++)
                {
                    // Create a new cell and add it to the Cells collection.
                    HtmlTableCell cell = new HtmlTableCell();

                    
#if MSSql
                    cell.Controls.Add(new LiteralControl(Common_SXD.TimeToYMD(sqlReader.GetValue(i).ToString())));
#elif MySql
                    string temp = Common_SXD.TimeToYMD(MysqlReader.GetValue(i).ToString());
                    cell.Controls.Add(new LiteralControl(Common_SXD.TimeToYMD(MysqlReader.GetValue(i).ToString())));
#endif
                    row.Cells.Add(cell);
                }
                {
                    HtmlTableCell cell = new HtmlTableCell();
                    cell.Controls.Add(new LiteralControl("<a href=./ApplicationEdit.aspx?Series_No=" +
                        sSeries_No.Trim() + ":Edit_Application:" + MysqlReader.GetValue(0).ToString().Trim() +
                             ">修改申请个案</a>"));
                    row.Cells.Add(cell);
                }

                Table_List_Applications.Rows.Add(row);
                j++;
                
            }
#if MSSql
            sqlReader.Close(); // 关闭sqDataReader
            sqlCnn.Close(); // 关闭数据库连接
#elif MySql
            MysqlReader.Close(); // 关闭sqDataReader
            MySqlconn.Close(); // 关闭数据库连接

#endif

        }

        /// <summary>
        /// 更新选定的院校（与注册管理共享的contract字段）
        /// 
        /// </summary>
        public bool Update_Selected_School()
        {
            ////////////////////////
            ///合同主键，申请个案类型，院校名，课程名
            ////////////////////////
            string temp;
            try
            {
                temp = Drop_College_Application.SelectedItem.Text.ToString().Trim();
            }
            catch (Exception err)
            {
                Response.Write("<li选择院校有误：" + err.ToString() + " ！</li>");
                return false;
            }
            if (temp.Length <= 0)
            {
                Response.Write("<li选择院校有误！</li>");
                return false;
            }

            //合同信息写入数据库
            string sSql = "Update Contract Set";

            if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "长期课程1")
            {
                //长期课程院校1
                if (!Drop_College_Application.Enabled)
                {
                    Drop_College_Application.Enabled = true;
                    sSql = sSql_Add_DropDownList(Drop_College_Application, "College1_Degree_courses", sSql);
                    Drop_College_Application.Enabled = false;
                }
                else
                    sSql = sSql_Add_DropDownList(Drop_College_Application, "College1_Degree_courses", sSql);

                if (!Drop_Course_Application.Enabled)
                {
                    Drop_Course_Application.Enabled = true;
                    sSql = sSql_Add_DropDownList(Drop_Course_Application, "Course1_Degree_courses", sSql);
                    Drop_Course_Application.Enabled = false;
                }
                else
                    sSql = sSql_Add_DropDownList(Drop_Course_Application, "Course1_Degree_courses", sSql);
                    
            }
            else if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "长期课程2")
            {
                if (!Drop_College_Application.Enabled)
                {
                    Drop_College_Application.Enabled = true;
                    sSql = sSql_Add_DropDownList(Drop_College_Application, "College2_Degree_courses", sSql);
                    Drop_College_Application.Enabled = false;
                }
                else
                    sSql = sSql_Add_DropDownList(Drop_College_Application, "College2_Degree_courses", sSql);

                sSql = sSql_Add_TextBox(TextBox_Course_Application, "Course2_Degree_courses", sSql);

            }
            else if(Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "学前语言课程")
            {
                if (!Drop_College_Application.Enabled)
                {
                    Drop_College_Application.Enabled = true;
                    sSql = sSql_Add_DropDownList(Drop_College_Application, "College_Pre_sessional", sSql);
                    Drop_College_Application.Enabled = false;
                }
                else
                    sSql = sSql_Add_DropDownList(Drop_College_Application, "College_Pre_sessional", sSql);

                sSql = sSql_Add_TextBox(TextBox_Course_Application, "Course_Pre_sessional", sSql);

            }
            else if (Drop_Type_Application.SelectedItem.Text.ToString().Trim() == "语言课程")
            {
                if (!Drop_College_Application.Enabled)
                {
                    Drop_College_Application.Enabled = true;
                    sSql = sSql_Add_DropDownList(Drop_College_Application, "College_Language_School", sSql);
                    Drop_College_Application.Enabled = false;
                }
                else
                    sSql = sSql_Add_DropDownList(Drop_College_Application, "College_Language_School", sSql);

                sSql = sSql_Add_TextBox(TextBox_Course_Application, "Course_Language_School", sSql);

            }

            sSql += "where Series_No = " + "'" + Label_Series_No.Text.Trim() + "'";
            /// 替换 where 左侧的逗号
            sSql = sSql.Replace(", where", " where");

#if MSSql
            Common_SXD.MSSql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#elif MySql
            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#endif

        return true;
        }

        /// <summary>
        /// 添加新申请人数据
        /// </summary>
        public void Insert_NewOne_Applicant()
        {
            ////////////////////////
            ///处理已经存在的申请人
            ////////////////////////
              
            Update_Applicant();

        }
        
        /// <summary>
        ///  初始化网页显示控件，顾问修改申请人数据。
        /// </summary>
        private void InitControls_Edit_Application()
        {
            false_Enable_All_Controls_Applicant();
            false_Enable_All_Controls_Application();

            if (!CheckBox_School_Application.Checked)
                Drop_Type_Application.Enabled = true;
            /*
            if (CheckBox_Course_Application.Checked)
            {
                Drop_Course_Application.Enabled = true;
                Drop_Course_Application.Visible = true;

                Drop_College_Application.Enabled = false;

                CheckBox_Course_Application.Enabled = true;
            }
            else
            {
                Drop_Course_Application.Enabled = false;
                Drop_Course_Application.Visible = true;
                if(CheckBox_Selected_Type_Application.Checked)
                    Drop_College_Application.Enabled = true;

                CheckBox_Course_Application.Enabled = true;

            }*/
            Drop_Writer_Application.Enabled = true;
            Drop_Net_Application.Enabled = true;
            TextBox_Date_Submit_Application.Enabled = true;
            TextBox_Date_Result_Application.Enabled = true;
            TextBox_Result_Application.Enabled = true;
            TextBox_ID_Application.Enabled = true;
            TextBox_User_Application.Enabled = true;
            TextBox_Password_User_Application.Enabled = true;

            CheckBox_Is_Active_Application.Enabled = true;
            CheckBox_Course_Application.Enabled = true;
            ///Drop_Type_Application.Enabled = true;

            Bt_OK.Enabled = true;
            ///Button_Edit_Application.Enabled = true;

           

        }

        /// <summary>
        ///  初始化网页显示控件，只读申请人数据。
        /// </summary>
        private void InitControls_ReadOnly()
        {
            false_Enable_All_Controls_Applicant();
            false_Enable_All_Controls_Application();
            Bt_OK.Enabled = true;
        }

        /// <summary>
        ///  初始化网页显示控件，修改申请人数据。
        /// </summary>
        private void InitControls_Edit_Applicant()
        {
            InitControls_NewOne();
            ///Button_Add_Application.Enabled = true;
            ///CheckBox_Add_Application.Enabled = true;
            Bt_OK.Enabled = true;
        }

        /// <summary>
        ///  初始化网页显示控件，新添加的客户数据。
        /// </summary>
        private void InitControls_NewOne()
        {
            false_Enable_All_Controls_Applicant();
            false_Enable_All_Controls_Application();

            TextBox_Course_Applicant.Enabled = true;
            TextBox_Date_Birth.Enabled = true;
            TextBox_University_Applicant_Learning.Enabled = true;
            TextBox_Course_Learning.Enabled = true;
            TextBox_Grade_Applicant.Enabled = true;
            Drop_Level_Application.Enabled = true;
            TextBox_Mobile_Applicant.Enabled = true;
            TextBox_Email_Student_Applicant.Enabled = true;
            TextBox_Email_Student.Enabled = true;
            TextBox_Password_Email_Student_Applicant.Enabled = true;
            TextBox_Notes_Applicant.Enabled = true;

            Bt_OK.Enabled = true;
          

        }


        /// <summary>
        ///  关闭申请个案的页面交互控制。
        /// </summary>
        private void false_Enable_All_Controls_Application()
        {
            Drop_College_Application.Enabled = false;
            TextBox_Course_Application.Enabled = false;
            Drop_Course_Application.Enabled = false;
            TextBox_Writer_Application.Enabled = false;
            Drop_Writer_Application.Enabled = false;
            TextBox_Net_Application.Enabled = false;
            Drop_Net_Application.Enabled = false;
            TextBox_Date_Submit_Application.Enabled = false;
            TextBox_Date_Result_Application.Enabled = false;
            TextBox_Result_Application.Enabled = false;
            TextBox_ID_Application.Enabled = false;
            TextBox_User_Application.Enabled = false;
            TextBox_Password_User_Application.Enabled = false;
            
            TextBox_Student_Name.Enabled = false;

            CheckBox_Is_Active_Application.Enabled = false;
            CheckBox_Course_Application.Enabled = false;
            Bt_OK.Enabled = false;
            Drop_Type_Application.Enabled = false;
        }

        /// <summary>
        ///  关闭申请人的页面交互控制。
        /// </summary>
        private void false_Enable_All_Controls_Applicant()
        {
            TextBox_Office.Enabled = false;
            TextBox_Student_Name.Enabled = false;
            TextBox_Destinations_OS.Enabled = false;
            TextBox_Date_First_Payment.Enabled = false;
            TextBox_Student_Type.Enabled = false;
            TextBox_YearMonth_Start.Enabled = false;
            TextBox_Date_Birth.Enabled = false;
////////////////////////

            TextBox_Course_Applicant.Enabled = false;
            TextBox_University_Applicant_Learning.Enabled = false;
            TextBox_Course_Learning.Enabled = false;
            TextBox_Grade_Applicant.Enabled = false;
            Drop_Level_Application.Enabled = false;
            TextBox_Mobile_Applicant.Enabled = false;
            TextBox_Email_Student_Applicant.Enabled = false;
            TextBox_Email_Student.Enabled = false;
            TextBox_Password_Email_Student_Applicant.Enabled = false;
            TextBox_Notes_Applicant.Enabled = false;
            

            CheckBox_Is_Active_Applicant.Enabled = false;
            ///CheckBox_Is_New.Enabled = false;
            CheckBox_AllowedSameName.Enabled = false;

            ///Button_Add_Application.Enabled = false;
            ///CheckBox_Add_Application.Enabled = false;
            ///Button_Edit_Application.Enabled = false;
            ///Bt_OK.Enabled = false;
            
        }

        /// <summary>
        /// 初始化，只读申请个案信息
        /// </summary>
        public void Init_ReadOnly_Application()
        {
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Is_Frontline_Agent"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);
            /*
            InitControls_NewOne();

            string sSql = "select * from contract where Series_No = " + "'" + sSeries_No + "'";

            DataSet bsDataSet = Init_DataSet("Contract", sSql);
            if (bsDataSet.Tables["Contract"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Contract"].Rows[0]; // 只有一行
                // 显示用户信息
                TextBox_Office.Text = rowUser["Office"].ToString().Trim();
                TextBox_Student_Name.Text = rowUser["Student_Name"].ToString().Trim();
                TextBox_Destinations_OS.Text = rowUser["Destinations_OS"].ToString().Trim();
                TextBox_Student_Type.Text = rowUser["Student_Type"].ToString().Trim();
                TextBox_Date_First_Payment.Text = Common_SXD.TimeToYMD(rowUser["Date_First_Payment"].ToString().Trim());
                TextBox_Student_Name.Text = rowUser["Student_Name"].ToString().Trim();

                TextBox_Course_Applicant.Text = rowUser["Course_Applicant"].ToString().Trim();
                TextBox_University_Applicant_Learning.Text = rowUser["University_Applicant_Learning"].ToString().Trim();
                TextBox_Course_Learning.Text = rowUser["Course_Learning"].ToString().Trim();
                TextBox_Grade_Applicant.Text = rowUser["Grade_Applicant"].ToString().Trim();

                ///Level_Application   varchar  (50)  NULL, 
                Common_SXD.SetSelectedIndex(rowUser["Level_Application"].ToString().Trim(), Drop_Level_Application);

                TextBox_Mobile_Applicant.Text = rowUser["Mobile_Applicant"].ToString().Trim();
                TextBox_Email_Student_Applicant.Text = rowUser["Email_Student_Applicant"].ToString().Trim();
                TextBox_Notes_Applicant.Text = rowUser["Notes_Applicant"].ToString().Trim();



            }
        */
        }

        /// <summary>
        /// 初始化，修改申请个案 信息
        /// </summary>
        public void Init_Edit_Application()
        {
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Is_Frontline_Agent"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

            //读取申请人信息
            Init_Edit_Applicant();
            InitControls_Edit_Application();
            ///Label_Tab_Applicant.Text = "";
            ///Label_Tab_Application.Text = "修改申请个案信息：" + Drop_Type_Application.SelectedItem.Text;


            string sSql = "select * from Applications where Series_No_Applications = " + "'" + sSeries_No_Application + "'";

            DataSet bsDataSet = Init_DataSet("Applications", sSql);
            if (bsDataSet.Tables["Applications"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Applications"].Rows[0]; // 只有一行
                // 显示用户信息
                TextBox_Result_Application.Text = rowUser["Result_Application"].ToString().Trim();

                ///TextBox_College_Application.Text = rowUser["College_Application"].ToString().Trim();
                Initialize_School();
                Common_SXD.SetSelectedIndex(rowUser["College_Application"].ToString().Trim(), Drop_College_Application);
                Initialize_Course();
                /*
                // 课程选择菜单初始化
                string temp;
                temp = "select * from Courses_Schools where Course like '无' or School like '" + Double_Single_Quotation(Drop_College_Application.SelectedItem.Text.Trim()) + "' order by Course asc";
                Common_SXD.Init_DropDownList(Drop_Course_Application, "Course", temp);
                */
                

                TextBox_Course_Application.Text = rowUser["Course_Application"].ToString().Trim();
                Common_SXD.SetSelectedIndex(rowUser["Course_Application"].ToString().Trim(), Drop_Course_Application);

                TextBox_Writer_Application.Text = rowUser["Writer_Application"].ToString().Trim();
                Common_SXD.SetSelectedIndex(rowUser["Writer_Application"].ToString().Trim(), Drop_Writer_Application);
                TextBox_Net_Application.Text = rowUser["Net_Application"].ToString().Trim();
                Common_SXD.SetSelectedIndex(rowUser["Net_Application"].ToString().Trim(), Drop_Net_Application);

                TextBox_Password_User_Application.Text = rowUser["Password_User_Application"].ToString().Trim();
                TextBox_User_Application.Text = rowUser["User_Application"].ToString().Trim();
                TextBox_Notes_Application.Text = rowUser["Notes_Application"].ToString().Trim();
                TextBox_ID_Application.Text = rowUser["ID_Application"].ToString().Trim();

                TextBox_Date_Submit_Application.Text = Common_SXD.TimeToYMD(rowUser["Date_Submit_Application"].ToString().Trim());
                TextBox_Date_Result_Application.Text = Common_SXD.TimeToYMD(rowUser["Date_Result_Application"].ToString().Trim());

                Read_CheckBox(CheckBox_Is_Active_Application, "Is_Active", rowUser);
                
            }
            ///InitializeSchool_Course();
            InitializeDrop_Writer();
            Label_Tab_Applicant.Text = "";
            Label_Tab_Application.Text = "修改申请个案信息：" + Drop_Type_Application.SelectedItem.Text;
            List_Applications();    
        }

        /// <summary>
        /// 初始化，修改申请人数据
        /// </summary>
        public void Init_ReadOnly_Applicant()
        {
            Init_Edit_Applicant();
            false_Enable_All_Controls_Applicant();
            Label_Tab_Applicant.Text = "读取申请人信息。请选择申请个案序号后修改信息，或者添加申请个案。";
            Label_Tab_Application.Text = "";

        }

        /// <summary>
        /// 初始化，修改申请人数据
        /// </summary>
        public void Init_Edit_Applicant()
        {
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Is_Frontline_Agent"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

            InitControls_NewOne();
            InitControls_Edit_Applicant();
            Label_Tab_Applicant.Text = "修改申请人信息";
            Label_Tab_Application.Text = "";

            string sSql = "select * from contract where Series_No = " + "'" + sSeries_No + "'";

            DataSet bsDataSet = Init_DataSet("Contract", sSql);
            if (bsDataSet.Tables["Contract"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Contract"].Rows[0]; // 只有一行
                // 显示用户信息
                TextBox_Office.Text = rowUser["Office"].ToString().Trim();
                TextBox_Student_Name.Text = rowUser["Student_Name"].ToString().Trim();
                TextBox_Destinations_OS.Text = rowUser["Destinations_OS"].ToString().Trim();
                TextBox_Student_Type.Text = rowUser["Student_Type"].ToString().Trim();
                TextBox_YearMonth_Start.Text = rowUser["YearMonth_Start"].ToString().Trim();
                TextBox_Date_First_Payment.Text = Common_SXD.TimeToYMD(rowUser["Date_First_Payment"].ToString().Trim());
                TextBox_Student_Name.Text = rowUser["Student_Name"].ToString().Trim();
                TextBox_Date_Birth.Text = rowUser["Date_Birth"].ToString().Trim();
                TextBox_Date_Birth.Text = Common_SXD.TimeToYMD(TextBox_Date_Birth.Text);
                if (TextBox_Date_Birth.Text == "")
                    TextBox_Date_Birth.Enabled = true;
                else
                    TextBox_Date_Birth.Enabled = false;

                TextBox_Course_Applicant.Text = rowUser["Course_Applicant"].ToString().Trim();
                TextBox_University_Applicant_Learning.Text = rowUser["University_Applicant_Learning"].ToString().Trim();
                TextBox_Course_Learning.Text = rowUser["Course_Learning"].ToString().Trim();
                TextBox_Grade_Applicant.Text = rowUser["Grade_Applicant"].ToString().Trim();

                ///Level_Application   varchar  (50)  NULL, /*申请层次*/
                Common_SXD.SetSelectedIndex(rowUser["Level_Application"].ToString().Trim(), Drop_Level_Application);

                TextBox_Mobile_Applicant.Text = rowUser["Mobile_Applicant"].ToString().Trim();
                TextBox_Email_Student_Applicant.Text = rowUser["Email_Student_Applicant"].ToString().Trim();
                TextBox_Email_Student.Text = rowUser["Email_Student"].ToString().Trim();
                TextBox_Password_Email_Student_Applicant.Text = rowUser["Password_Email_Student_Applicant"].ToString().Trim();
                TextBox_Notes_Applicant.Text = rowUser["Notes_Applicant"].ToString().Trim();



            }
            InitializeDrop_Writer();

            List_Applications();
        }

        /// <summary>
        /// 初始化，添加新的申请人数据
        /// </summary>
        public void Init_NewOne_Applicant()
        {
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            ///bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            ///bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Frontline_Agent = Convert.ToBoolean(Session["Is_Frontline_Agent"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

            InitControls_NewOne();
            Label_Tab_Applicant.Text = "添加申请人";
            Label_Tab_Application.Text = "";

            string sSql = "select * from contract where Series_No = " + "'" + sSeries_No + "'";
           
            DataSet bsDataSet = Init_DataSet("Contract", sSql);
            if (bsDataSet.Tables["Contract"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Contract"].Rows[0]; // 只有一行
                // 显示用户信息
                TextBox_Office.Text = rowUser["Office"].ToString().Trim();
                TextBox_Student_Name.Text = rowUser["Student_Name"].ToString().Trim();
                TextBox_Destinations_OS.Text = rowUser["Destinations_OS"].ToString().Trim();
                TextBox_Student_Type.Text = rowUser["Student_Type"].ToString().Trim();
                TextBox_Date_First_Payment.Text = Common_SXD.TimeToYMD(rowUser["Date_First_Payment"].ToString().Trim());
                TextBox_Student_Name.Text = rowUser["Student_Name"].ToString().Trim();

                TextBox_Date_Birth.Text = rowUser["Date_Birth"].ToString().Trim();
                TextBox_Date_Birth.Text = Common_SXD.TimeToYMD(TextBox_Date_Birth.Text);
                if (TextBox_Date_Birth.Text == "")
                    TextBox_Date_Birth.Enabled = true;
                else
                    TextBox_Date_Birth.Enabled = false;

                TextBox_Course_Applicant.Text = rowUser["Course_Applicant"].ToString().Trim();
                TextBox_University_Applicant_Learning.Text = rowUser["University_Applicant_Learning"].ToString().Trim();
                TextBox_Course_Learning.Text = rowUser["Course_Learning"].ToString().Trim();
                TextBox_Grade_Applicant.Text = rowUser["Grade_Applicant"].ToString().Trim();

	            ///Level_Application   varchar  (50)  NULL, /*申请层次*/
                Common_SXD.SetSelectedIndex(rowUser["Level_Application"].ToString().Trim(), Drop_Level_Application);

	            TextBox_Mobile_Applicant.Text = rowUser["Mobile_Applicant"].ToString().Trim();
                TextBox_Email_Student_Applicant.Text = rowUser["Email_Student_Applicant"].ToString().Trim();
                TextBox_Email_Student.Text = rowUser["Email_Student"].ToString().Trim();
                TextBox_Password_Email_Student_Applicant.Text = rowUser["Password_Email_Student_Applicant"].ToString().Trim();
                TextBox_Notes_Applicant.Text = rowUser["Notes_Applicant"].ToString().Trim();



            }
            ///InitializeDrop_Writer();

        }

        /// <summary>
        /// 添加申请户数据
        /// </summary>
        public void Insert_New_Application()
        {

            string sSql = "insert into Applications (  Series_No, Type_Application, Writer_Application, Net_Application, Is_Active ) values(";
            sSql += "'" + Label_Series_No.Text.Trim() + "', ";
            sSql += "'长期课程1', '无', '无',1 ";
            sSql += " )";
#if MySql
            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#elif MSSql
            Common_SXD.MSSql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#endif

        }

        protected void Bt_OK_Click(object sender, EventArgs e)
        {
            if (CheckBox_Add_Application.Checked)
            {
                Insert_New_Application();
                List_Applications();
                CheckBox_Add_Application.Checked = false;
                false_Enable_All_Controls_Application();
            }
            else if (Label_Series_No_Applications.Text.Trim() == "")
            {/// 申请个案编号为空，修改申请人信息。
                Update_Applicant();
            }
            else
            {
                ///Update_Application();
                ///如果选定最终去向，更新contract表中的对应记录
                ///
                if (CheckBox_Selected_School.Checked)
                {
                    if (Update_Selected_School())
                    {
                        Update_Application();
                        Init_Edit_Application();
                    }
                    else
                    {
                        Response.Write("<li选择院校有误！</li>");
                    }
                    CheckBox_Selected_School.Checked = false;
                }
                else
                {
                    Update_Application();
                    
                }
                Init_Edit_Application();
            }
            
            List_Applications();

        }

        protected void Drop_Writer_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Drop_College_Application_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Drop_Course1_Degree_courses_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button_Add_Application_Click(object sender, EventArgs e)
        {
            Insert_New_Application();
            List_Applications();
        }

        protected void Button_Edit_Application_Click(object sender, EventArgs e)
        {
            /// 添加/修改申请人
            Update_Application();
            Init_Edit_Application();
        }

        protected void Drop_Net_Application_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox_Net_Application.Text = Drop_Net_Application.SelectedItem.Text.ToString().Trim();
        }

        protected void Drop_Writer_Application_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox_Writer_Application.Text = Drop_Writer_Application.SelectedItem.Text.ToString().Trim();
        }

        

    }
}